package com.example.blog.service.impl;

import com.example.blog.dao.BlogMapper;
import com.example.blog.dao.CommentMapper;
import com.example.blog.pojo.Blog;
import com.example.blog.pojo.query.SearchBlog;
import com.example.blog.service.BlogService;
import com.example.blog.utils.MarkdownUtils;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * name: 东北宋小宝
 * date: 2020/8/17
 */
@Service
@Transactional
public class BlogServiceImpl implements BlogService {

    @Resource
    private BlogMapper blogMapper;
    @Resource
    private CommentMapper commentMapper;

    @Override
    public PageInfo getPage(Integer page, Integer num) {
        page = (page == null) ? 1 : page;
        PageHelper.startPage(page, num, true);
        List<Blog> all = blogMapper.findAll(new HashMap());
        PageInfo<Blog> pageInfo = new PageInfo<Blog>(all);
        return pageInfo;
    }

    public PageInfo queryPage(Integer page, Integer num, SearchBlog searchBlog){
        page = (page == null) ? 1 : page;
        PageHelper.startPage(page, num);
        List<Blog> all = blogMapper.findAll(searchBlog);
        PageInfo<Blog> pageInfo = new PageInfo<Blog>(all);
        return pageInfo;
    }

    @Override
    public Map getInformation() {
        Map<String, Object> map = new ConcurrentHashMap<String, Object>();
        map.put("BlogNum",PageHelper.count(()->blogMapper.findAll(map)));
        map.put("ViwesNum",blogMapper.getAllViews());
        map.put("CommentNum",PageHelper.count(()->commentMapper.findAll(map)));
        map.put("MessageNum",10);
        return map;
    }

    @Override
    public Blog getBlog(Integer id) {
        if (id == null) {
            return new Blog();
        }
        Map map = new HashMap(1);
        map.put("id",id);
        Blog blog = blogMapper.findBlog(map);
        return blog;
    }

    @Override
    public Boolean delete(Integer id) {
        Boolean flag = false;
        Map map = new HashMap(1);
        map.put("id",id);
        Blog blog = blogMapper.findBlog(map);
        if (blog != null){
            blogMapper.delBlog(id);
            flag = true;
        }
        return flag;
    }

    @Override
    public void saveBlog(Blog blog) {
        blog.setUpdateTime(new Date());
        if (blog.getId() == null) {
            blog.setCreatTime(new Date());
            blog.setViews(0);
            blogMapper.insertBlog(blog);
        } else {
            blogMapper.updateBlog(blog);
        }
    }

    @Override
    public List<Blog> getBlogOrderBy() {
        PageHelper.startPage(1,4);
        PageHelper.orderBy("creat_time DESC");
        List<Blog> all = blogMapper.findAll(new HashMap());
        PageInfo<Blog> pageInfo = new PageInfo<>(all);
        return pageInfo.getList();
    }

    @Override
    public List<Blog> getAllBlog() {
        List<Blog> all = blogMapper.findAll(new ConcurrentHashMap());
        return all;
    }

}
